﻿//ExchangeBoardComboBox.cs
//Copyright (c) 2013 StockSharp LLC, all rights reserved.
//This code module is part of StockSharp library.
//This code is licensed under the GNU GENERAL PUBLIC LICENSE Version 3.
//See the file License.txt for the license details.
//More info on: http://stocksharp.com

namespace StockSharp.Xaml
{
	using System.Linq;
	using System.Windows;
	using System.Windows.Controls;
	using System.Windows.Data;

	using Ecng.Configuration;

	using StockSharp.Algo.Storages;
	using StockSharp.BusinessEntities;

	/// <summary>
	/// Комбо элемент для выбора биржевой площадки.
	/// </summary>
	public class ExchangeBoardComboBox : ComboBox
	{
		/// <summary>
		/// Создать <see cref="ExchangeBoardComboBox"/>.
		/// </summary>
		public ExchangeBoardComboBox()
		{
			ItemsSource = new CompositeCollection
			{
				new ComboBoxItem
				{
					IsEnabled = false,
					Content = "Выберите биржевую площадку"
				},
				new CollectionContainer
				{
					Collection = ConfigManager.GetService<IEntityRegistry>().ExchangeBoards.ToArray()
				}
			};
		}

		/// <summary>
		/// <see cref="DependencyProperty"/> для <see cref="SelectedExchangeBoard"/>.
		/// </summary>
		public static readonly DependencyProperty SelectedExchangeBoardProperty =
			 DependencyProperty.Register("SelectedExchangeBoard", typeof(ExchangeBoard), typeof(ExchangeBoardComboBox),
				new FrameworkPropertyMetadata(null, OnSelectedExchangeBoardPropertyChanged));

		/// <summary>
		/// Выбранная биржевая площадка.
		/// </summary>
		public ExchangeBoard SelectedExchangeBoard
		{
			get { return (ExchangeBoard)GetValue(SelectedExchangeBoardProperty); }
			set { SetValue(SelectedExchangeBoardProperty, value); }
		}

		private static void OnSelectedExchangeBoardPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
		{
			var exchange = (ExchangeBoard)e.NewValue;
			var editor = (ExchangeBoardComboBox)source;

			editor.SelectedItem = exchange;
		}

		/// <summary>
		/// Обработчик события смены выбранного элемента.
		/// </summary>
		/// <param name="e">Параметр события.</param>
		protected override void OnSelectionChanged(SelectionChangedEventArgs e)
		{
			base.OnSelectionChanged(e);
			SelectedExchangeBoard = (ExchangeBoard)SelectedItem;
		}
	}
}